<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Blog extends Admin_Controller {

	public function __construct()
	{
        parent::__construct();

        $this->load->model(array('backend/blog_model', 'backend/blog_category_model'));
    }

	public function index()
	{
        $limit = 20;
        $offset = _get('per_page', 0);

        $options = array(
            'category_id' => _get('category_id', 'all')
        );

        $result = $this->blog_model->gets($options, $limit, $offset);

        $pagination = create_pagination(uri_string() . "?category_id={$options['category_id']}", $result['total_rows'], $limit);

        $categories = array('all' => 'Tüm Kategoriler') + $this->blog_category_model->tree_selectbox();

        $data = array(
            'result' => $result,
            'status' => array('Pasif', 'Aktif'),
            'categories' => $categories,
            'pagination' => $pagination
        );

        $this->template->load('blog/index', $data);
    }

	public function add()
	{
		if (empty($_POST))
		{
            // categories
            $categories = $this->blog_category_model->get_path();

            $data = array(
                'categories' => $categories
            );
            $this->template->load('blog/add', $data);
        } else {

            // VALIDATION
            $this->load->library('form_validation');
            
            $this->form_validation->set_rules("categories[]", 'Kategori', 'trim|required|numeric');
            $this->form_validation->set_rules("image", 'Kapak Resmi', 'trim|required|callback__check_file');
            $this->form_validation->set_rules('title', 'Başlık', 'trim|required|xss_clean|max_length[255]');
            $this->form_validation->set_rules('description', 'İçerik', 'trim|required');

			if ($this->form_validation->run() === FALSE)
			{
				ajax_response($this->form_validation->error_string());
            }

            // CONTINUE
            
            $data = array(
                'main' => array(
                    'image' => global_filepath($this->input->post('image')),
                    'title' => $this->input->post('title'),
                    'slug' => url_title($this->input->post('title')),
                    'description' => $this->input->post('description')
                    ),
                'categories' => $_POST['categories']
            );

            $this->blog_model->add($data);
			
			$msg = 'Gönderi başarıyla eklendi.';
			set_notify($msg);
			
			ajax_response('Gönderi başarıyla eklendi', FALSE, site_url('backend/blog/index'));
        }
    }

	public function edit($id)
	{
		if (empty($_POST))
		{
            $blog = $this->blog_model->get($id) OR show_error('Gönderi bulunamadı');

            // blog categories selectbox
            $this->load->model('backend/blog_category_model');
            $categories = $this->blog_category_model->get_path();

            // categories of blog
            $blog_categories = $this->blog_model->get_categories($blog->id);

            $data = array(
                'blog' => $blog,
                'categories' => $categories,
                'blog_categories' => $blog_categories
            );

            $this->template->load('blog/edit', $data);
            
        } else {
            
            // VALIDATION
            $this->load->library('form_validation');
            
            $this->form_validation->set_rules('blog_id', 'Blog ID', 'trim|required|numeric');
            $this->form_validation->set_rules("categories[]", 'Kategori', 'trim|required|numeric');
            $this->form_validation->set_rules("image", 'Kapak Resmi', 'trim|required|callback__check_file');
            $this->form_validation->set_rules('title', 'Başlık', 'trim|required|xss_clean|max_length[255]');
            $this->form_validation->set_rules('description', 'İçerik', 'trim|required');

			if ($this->form_validation->run() === FALSE)
			{
				ajax_response($this->form_validation->error_string());
            }

            // CONTINUE
            
            $data = array(
                'main' => array(
                    'image' => global_filepath($this->input->post('image')),
                    'title' => $this->input->post('title'),
                    'slug' => url_title($this->input->post('title')),
                    'description' => $this->input->post('description')
                    ),
                'categories' => $_POST['categories']
            );

            $this->blog_model->edit($data, $_POST['blog_id']);
			
			$msg = 'Gönderi başarıyla düzenlendi.';
			set_notify($msg);

			ajax_response('Gönderi başarıyla düzenlendi', FALSE, site_url('backend/blog/index'));
        }
    }

	public function delete($id)
	{
        $this->blog_model->delete($id);
		
		$msg = 'Gönderi başarıyla silindi.';
		set_notify($msg);

        redirect($this->input->server('HTTP_REFERER'));
    }

    /**
     * ----------------------------------------------------------------------
     * HELPERS
     * ----------------------------------------------------------------------
     **/

    /**
     * ----------------------------------------------------------------------
     * CALLBACKS
     * ----------------------------------------------------------------------
     **/
	public function _check_file($file)
	{
        $file = global_filepath($file);

        if (is_file($file))
            return true;

        $this->form_validation->set_message('_check_file', 'Kapak resmi geçersiz.');
        return FALSE;
    }

}

/* End file of blog.php */
